package com.jf.mall.web;

import com.google.gson.Gson;
import com.jf.mall.domain.ShoppingCart;
import com.jf.mall.domain.User;
import com.jf.mall.service.ShoppingCartService;
import com.jf.mall.utils.DataUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-10-02
 * Time:5:26
 */
@WebServlet(name = "ShoppingCartServlet", urlPatterns = "/shoppingCartServlet")
public class ShoppingCartServlet extends BasicServlet {
    private ShoppingCartService shoppingCartService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        shoppingCartService = new ShoppingCartService();
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        createCart(req, resp);
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        HttpSession session = req.getSession();
        ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute("shoppingCart");
        User user = (User) session.getAttribute("user");
        //if (user == null) {
        //    //未登录状态 1.购物车为空 2.购物车属于用户 3.else
        //    if (shoppingCart == null || shoppingCart.getUserId() != null) {
        //        shoppingCart = new ShoppingCart();
        //        session.setAttribute("shoppingCart", shoppingCart);
        //    }
        //    shoppingCartService.addCommodity(id, shoppingCart);
        //} else {
        //    //
        //    if (shoppingCart == null || shoppingCart.getUserId() == null) {
        //        shoppingCart = new ShoppingCart(user.getId());
        //        shoppingCartService.updateShoppingCart(shoppingCart);
        //        session.setAttribute("shoppingCart", shoppingCart);
        //    }
        //    shoppingCartService.addCommodity(id, shoppingCart);
        //}
        shoppingCartService.addCommodity(id, shoppingCart);
        resp.sendRedirect(req.getHeader("referer"));
    }

    private void addByAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        createCart(req, resp);
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        HttpSession session = req.getSession();
        ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute("shoppingCart");
        User user = (User) session.getAttribute("user");
        shoppingCartService.addCommodity(id, shoppingCart);
        Map<String, Object> json = new HashMap<>();
        json.put("totalCount", shoppingCart.getTotalCount());
        Gson gson = new Gson();
        gson.toJson(json, resp.getWriter());
    }
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        HttpSession session = req.getSession();
        ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute("shoppingCart");
        if (shoppingCart != null) shoppingCartService.deleteCommodity(id, shoppingCart);
        resp.sendRedirect(req.getHeader("referer"));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        int count = DataUtils.parseInt(req.getParameter("count"), 0);
        HttpSession session = req.getSession();
        ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute("shoppingCart");
        if (shoppingCart != null) shoppingCartService.updateCommodityCount(id, shoppingCart, count);
        resp.sendRedirect(req.getHeader("referer"));

    }

    private void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        if (user != null) {
            shoppingCartService.clearUserShoppingCart(user);
        } else {
            session.setAttribute("shoppingCart", new ShoppingCart());
        }
        //resp.sendRedirect("views/customer/cart.jsp");
        resp.sendRedirect(req.getHeader("referer"));
    }

    private void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        createCart(req, resp);
        HttpSession session = req.getSession();
        ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute("shoppingCart");
        shoppingCartService.updateShoppingCart(shoppingCart);
        //resp.sendRedirect(req.getServletContext().getContextPath() + "/views/customer/cart.jsp");
        req.getRequestDispatcher("views/customer/cart.jsp").forward(req, resp);
    }

    private void createCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute("shoppingCart");
        boolean isUserCart = shoppingCart != null && shoppingCart.getUserId() != null;
        if (user == null && (shoppingCart == null ||isUserCart)) {
            shoppingCart = new ShoppingCart();
            session.setAttribute("shoppingCart", shoppingCart);
        } else if (user != null && !isUserCart) {
            shoppingCart = new ShoppingCart(user.getId());
            shoppingCartService.updateShoppingCart(shoppingCart);
            session.setAttribute("shoppingCart", shoppingCart);
        }//todo (user != null && shoppingCart.userId == null)
    }
}
